package com.huangwenshuo.futurestarschoolmanagement.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.Page;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.Result;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.StudentVo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.UsersVo;
import com.huangwenshuo.futurestarschoolmanagement.service.StudentsService;
import com.huangwenshuo.futurestarschoolmanagement.service.UsersService;
import com.huangwenshuo.futurestarschoolmanagement.utils.ResUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huangwenshuo
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/stu")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    @Autowired
    private UsersService usersService;

    @GetMapping("/getByPages")
    public Result getStuPages(Page page) {
        List<StudentVo> studentVos = studentsService.getStuPages(page);
        Integer total = studentsService.getStuTotal(page);
        List<String> classNames = studentsService.getClassNames(page);
        List<String> positionNames = studentsService.getPositionNames(page);
        Map<String, Object> res = new HashMap<>();
        res.put("stus", studentVos);
        res.put("total", total);
        res.put("classNames", classNames);
        res.put("positionNames", positionNames);
//        Principal r = request.getUserPrincipal();
        return ResUtils.result(res);
    }

    @PostMapping("/update")
    public Result updateStu(@RequestBody StudentVo studentVo) {
        try {
            studentsService.updateStu(studentVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResUtils.result("失败");
        }
        return ResUtils.result("success");
    }

    @GetMapping("/delete")
    public Result delete(String uid) {
        try {
            studentsService.deleteStu(uid);
        } catch (Exception e) {
            return ResUtils.result(e.getMessage() + "");
        }
        return ResUtils.result("success");
    }
    @GetMapping("/findUsers")
    private Result findUsers(@RequestParam(name = "realName",required = false) String realName,@RequestParam(name = "phone",required = false) String phone){
        List<UsersVo> usersVo = null;
        if((realName == null || realName == "") && (phone == null || phone == "") )
        {
            return ResUtils.result(new ArrayList<UsersVo>());
        }
        try {
             usersVo =usersService.findUsers(realName,phone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResUtils.result("失败");
        }
        return ResUtils.result(usersVo);
    }

    @PostMapping("/addStu")
    public Result addStu(@RequestBody StudentVo studentVo) {
        try {
            studentsService.addStu(studentVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResUtils.result(e.getMessage() + "");
        }
        return ResUtils.result("success");
    }

    @GetMapping("/exportStu" )
    public void exportStu(Page page,HttpServletResponse response) {
        studentsService.exportStu(page,response);
    }

}

